package com.jit.monitor.presenter;

import com.jit.baselib.bean.response.BaseListResponse;
import com.jit.baselib.bean.response.BaseResponse;
import com.jit.baselib.ui.base.BaseActivity;
import com.jit.baselib.ui.base.BaseObserver;
import com.jit.baselib.ui.base.BasePresenter;
import com.jit.baselib.ui.base.ExceptionHandle;
import com.jit.monitor.base.ApiRetrofit;
import com.jit.monitor.commom.AppConstant;
import com.jit.monitor.model.CheckDataManager;
import com.jit.monitor.model.bean.EarlyWarning;
import com.jit.monitor.model.cache.UserCache;
import com.jit.monitor.model.request.WarnRequest;
import com.jit.monitor.view.WarnAreaAtView;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author crazyZhangxl on 2019/1/21.
 * Describe:
 */
public class WarnAtPresenter extends BasePresenter<WarnAreaAtView>{
    public WarnAtPresenter(BaseActivity context) {
        super(context);
    }


    public void setAlarmRange(List<String> warnList){
        // 进行翻译
        List<String> englishList = new ArrayList<>();
        for (int i=0;i<warnList.size();i++){
            String englishName = CheckDataManager.getEnglishName(warnList.get(i));
            if (null != englishName){
                englishList.add(englishName);
            }
        }
        WarnRequest warnRequest = new WarnRequest();
        warnRequest.setConfigParams(englishList);
        warnRequest.setUserId(String.valueOf(UserCache.getUserID()));

        ApiRetrofit.getInstance().setAlarmArea(warnRequest)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        if (baseResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                            // 设置内容显示--
                            getView().setRangSuccess(warnList);
                        }else {
                            getView().setRangFailure(baseResponse.getMsg());
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        getView().setRangFailure(exception.getErrorInfo());
                    }
                });
    }

    public void queryRange(){
        getView().showLoadingDialog();
        ApiRetrofit.getInstance().queryAlarmRange()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseListResponse<EarlyWarning>>() {
                    @Override
                    public void onNext(BaseListResponse<EarlyWarning> earlyWarningBaseListResponse) {
                        if (earlyWarningBaseListResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                            List<EarlyWarning> data = earlyWarningBaseListResponse.getData();
                            if (data == null ||data.size() == 0){
                                getView().queryRangSuccess(null);
                            }else {
                                List<String> mList = new ArrayList<>();
                                for (int i=0;i<data.size();i++){
                                    mList.add(CheckDataManager.getCheckChineseName(data.get(i).getWarningRang()));
                                }
                                getView().queryRangSuccess(mList);
                            }
                        }else {
                            getView().queryRangFailure(earlyWarningBaseListResponse.getMsg());
                        }
                        getView().hideLoadingDialog();
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        getView().queryRangFailure(exception.getErrorInfo());
                        getView().hideLoadingDialog();
                    }
                });
    }
}
